home *** CD-ROM | disk | FTP | other *** search
/ Enigma Amiga Life 109 / EnigmaAmiga109CD.iso / dalla rivista / host contacted / jikes.lha / jikes-1.11 / src / unzip.h < prev    next >
C/C++ Source or Header  |  2000-01-16  |  17KB  |  374 lines

  1. // $Id: unzip.h,v 1.3 1999/08/26 15:34:10 shields Exp $
  2. #ifndef unzip_INCLUDED
  3. #define unzip_INCLUDED
  4.  
  5. #include "config.h"
  6.  
  7. //
  8. // NOTE: Jikes incorporates compression code from the Info-ZIP
  9. // group. There are no extra charges or costs due to the use of
  10. // this code, and the original compression sources are freely
  11. // available from http://www.cdrom/com/pub/infozip/ or
  12. // ftp://ftp.cdrom.com/pub/infozip/ on the Internet.
  13. // The sole use by Jikes of this compression code is contained in the
  14. // files unzip.h and unzip.cpp, which are based on Info-ZIP's inflate.c and
  15. // associated header files.
  16. //
  17.  
  18. //
  19. // You can do whatever you like with this source file, though I would
  20. // prefer that if you modify it and redistribute it that you include
  21. // comments to that effect with your name and the date.  Thank you.
  22. // The abbreviated History list below includes the work of the
  23. // following:
  24. // M. Adler, G. Roelofs, J-l. Failly, J. Bush, C. Ghisler, A. Verheijen,
  25. // P. Kienitz, C. Spieler, S. Maxwell, J. Altman
  26. // Only the first and last entries from the original inflate.c are
  27. // reproduced here.
  28. //
  29.  
  30. //
  31. // History:
  32. // vers    date          who           what
  33. // ----  ---------  --------------  ------------------------------------
  34. //  a    ~~ Feb 92  M. Adler        used full (large, one-step) lookup table
  35. //  ...
  36. //  c16  20 Apr 97  J. Altman       added memzero(v[]) in huft_build()
  37. //
  38.  
  39. #define DFUNZIP /* needed for unzip compilation*/
  40. #include <stdio.h>
  41. #include <stdlib.h>
  42. #include <string.h>
  43.  
  44. //
  45. // inflate.c -- put in the public domain by Mark Adler
  46. // version c15c, 28 March 1997
  47. //
  48.  
  49. //
  50. // Inflate deflated (PKZIP's method 8 compressed) data.  The compression
  51. // method searches for as much of the current string of bytes (up to a
  52. // length of 258) in the previous 32K bytes.  If it doesn't find any
  53. // matches (of at least length 3), it codes the next byte.  Otherwise, it
  54. // codes the length of the matched string and its distance backwards from
  55. // the current position.  There is a single Huffman code that codes both
  56. // single bytes (called "literals") and match lengths.  A second Huffman
  57. // code codes the distance information, which follows a length code.  Each
  58. // length or distance code actually represents a base value and a number
  59. // of "extra" (sometimes zero) bits to get to add to the base value.  At
  60. // the end of each deflated block is a special end-of-block (EOB) literal/
  61. // length code.  The decoding process is basically: get a literal/length
  62. // code; if EOB then done; if a literal, emit the decoded byte; if a
  63. // length then get the distance and emit the referred-to bytes from the
  64. // sliding window of previously emitted data.
  65.  
  66. // There are (currently) three kinds of inflate blocks: stored, fixed, and
  67. // dynamic.  The compressor outputs a chunk of data at a time and decides
  68. // which method to use on a chunk-by-chunk basis.  A chunk might typically
  69. // be 32K to 64K, uncompressed.  If the chunk is uncompressible, then the
  70. // "stored" method is used.  In this case, the bytes are simply stored as
  71. // is, eight bits per byte, with none of the above coding.  The bytes are
  72. // preceded by a count, since there is no longer an EOB code.
  73.  
  74. // If the data are compressible, then either the fixed or dynamic methods
  75. // are used.  In the dynamic method, the compressed data are preceded by
  76. // an encoding of the literal/length and distance Huffman codes that are
  77. // to be used to decode this block.  The representation is itself Huffman
  78. // coded, and so is preceded by a description of that code.  These code
  79. // descriptions take up a little space, and so for small blocks, there is
  80. // a predefined set of codes, called the fixed codes.  The fixed method is
  81. // used if the block ends up smaller that way (usually for quite small
  82. // chunks); otherwise the dynamic method is used.  In the latter case, the
  83. // codes are customized to the probabilities in the current block and so
  84. // can code it much better than the pre-determined fixed codes can.
  85.  
  86. // The Huffman codes themselves are decoded using a multi-level table
  87. // lookup, in order to maximize the speed of decoding plus the speed of
  88. // building the decoding tables.  See the comments below that precede the
  89. // lbits and dbits tuning parameters.
  90.  
  91. // GRR:  return values(?)
  92. //         0  OK
  93. //         1  incomplete table
  94. //         2  bad input
  95. //         3  not enough memory
  96. //
  97.  
  98. //
  99. // If BMAX needs to be larger than 16, then h and x[] should be unsigned long.
  100. //
  101. #define BMAX 16         /* maximum bit length of any code (16 for explode) */
  102. #define N_MAX 288       /* maximum number of codes in any set */
  103.  
  104.  
  105. //
  106. // Notes beyond the 1.93a appnote.txt:
  107.  
  108. // 1. Distance pointers never point before the beginning of the output
  109. //    stream.
  110. // 2. Distance pointers can point back across blocks, up to 32k away.
  111. // 3. There is an implied maximum of 7 bits for the bit length table and
  112. //    15 bits for the actual data.
  113. // 4. If only one code exists, then it is encoded using one bit.  (Zero
  114. //    would be more efficient, but perhaps a little confusing.)  If two
  115. //    codes exist, they are coded using one bit each (0 and 1).
  116. // 5. There is no way of sending zero distance codes--a dummy must be
  117. //    sent if there are none.  (History: a pre 2.0 version of PKZIP would
  118. //    store blocks with no distance codes, but this was discovered to be
  119. //    too harsh a criterion.)  Valid only for 1.93a.  2.04c does allow
  120. //    zero distance codes, which is sent as one code of zero bits in
  121. //    length.
  122. // 6. There are up to 286 literal/length codes.  Code 256 represents the
  123. //    end-of-block.  Note however that the static length tree defines
  124. //    288 codes just to fill out the Huffman codes.  Codes 286 and 287
  125. //    cannot be used though, since there is no length base or extra bits
  126. //    defined for them.  Similarily, there are up to 30 distance codes.
  127. //    However, static trees define 32 codes (all 5 bits) to fill out the
  128. //    Huffman codes, but the last two had better not show up in the data.
  129. // 7. Unzip can check dynamic Huffman blocks for complete code sets.
  130. //    The exception is that a single code would not be complete (see #4).
  131. // 8. The five bits following the block type is really the number of
  132. //    literal codes sent minus 257.
  133. // 9. Length codes 8,16,16 are interpreted as 13 length codes of 8 bits
  134. //    (1+6+6).  Therefore, to output three times the length, you output
  135. //    three codes (1+1+1), whereas to output four times the same length,
  136. //    you only need two codes (1+3).  Hmm.
  137. //10. In the tree reconstruction algorithm, Code = Code + Increment
  138. //    only if BitLength(i) is not zero.  (Pretty obvious.)
  139. //11. Correction: 4 Bits: # of Bit Length codes - 4     (4 - 19)
  140. //12. Note: length code 284 can represent 227-258, but length code 285
  141. //    really is 258.  The last length deserves its own, short code
  142. //    since it gets used a lot in very redundant files.  The length
  143. //    258 is special since 258 - 3 (the min match length) is 255.
  144. //13. The literal/length and distance code bit lengths are read as a
  145. //    single stream of lengths.  It is possible (and advantageous) for
  146. //    a repeat code (16, 17, or 18) to go across the boundary between
  147. //    the two sets of lengths.
  148. //
  149.  
  150. #define PKZIP_BUG_WORKAROUND    /* PKZIP 1.93a problem--live with it */
  151.  
  152. //
  153. //  inflate.h must supply the unsigned char slide[WSIZE] array, the zvoid typedef
  154. //  (void if (void *) is accepted, else char) and the NEXTBYTE,
  155. //  FLUSH() and memzero macros.  If the window size is not 32K, it
  156. //  should also define WSIZE.  If INFMOD is defined, it can include
  157. //  compiled functions to support the NEXTBYTE and/or FLUSH() macros.
  158. //  There are defaults for NEXTBYTE and FLUSH() below for use as
  159. //  examples of what those functions need to do.  Normally, you would
  160. //  also want FLUSH() to compute a crc on the data.
  161.  
  162. //  This module uses the external functions malloc() and free() (and
  163. //  probably memset() or bzero() in the memzero() macro).  Their
  164. //  prototypes are normally found in <string.h> and <stdlib.h>.
  165. //
  166.  
  167. /* #define DEBUG */
  168.  
  169.  
  170. #ifndef WSIZE           /* default is 32K */
  171. #  define WSIZE 0x8000  /* window size--must be a power of two, and at least */
  172. #endif                  /* 32K for zip's deflate method */
  173.  
  174. #  define wsize WSIZE       /* wsize is a constant */
  175.  
  176.  
  177. #ifndef NEXTBYTE        /* default is to simply get a byte from stdin */
  178. /* default for   define NEXTBYTE is  getchar() */
  179. #if defined(UNIX_FILE_SYSTEM) || defined(AMIGAOS_FILE_SYSTEM)
  180.     #define NEXTBYTE getc(global_file)
  181. #elif defined(WIN32_FILE_SYSTEM)
  182.     #define NEXTBYTE ((u1) (*global_file++))
  183. #endif
  184. #endif
  185.  
  186. #ifndef MESSAGE   /* only used twice, for fixed strings--NOT general-purpose */
  187. #  define MESSAGE(str,len,flag)  fprintf(stderr,(char *)(str))
  188. #endif
  189.  
  190. #ifndef FLUSH           /* default is to simply write the buffer to stdout */
  191. /* default   define FLUSH(n) is fwrite(slide_buffer, 1, n, stdout)   return value not used */
  192. #define FLUSH(n) memcpy(global_bufferp, slide_buffer, n); global_bufferp += n
  193. #endif
  194. /* Warning: the fwrite above might not work on 16-bit compilers, since
  195.    0x8000 might be interpreted as -32,768 by the library function. */
  196.  
  197. #ifndef Trace
  198. #  ifdef DEBUG
  199. #    define Trace(x) fprintf x
  200. #  else
  201. #    define Trace(x)
  202. #  endif
  203. #endif
  204.  
  205.  
  206. /*---------------------------------------------------------------------------*/
  207.  
  208. // Macros for inflate() bit peeking and grabbing.
  209. // The usage is:
  210. //
  211. //      NEEDBITS(j)
  212. //      x = b & mask_bits[j];
  213. //
  214. //      DUMPBITS(j)
  215. // where NEEDBITS makes sure that b has at least j bits in it, and
  216. // DUMPBITS removes the bits from b.  The macros use the variable k
  217. // for the number of bits in b.  Normally, b and k are register
  218. // variables for speed and are initialized at the begining of a
  219. // routine that uses these macros from a global bit buffer and count.
  220. //
  221. // In order to not ask for more bits than there are in the compressed
  222. // stream, the Huffman tables are constructed to only ask for just
  223. // enough bits to make up the end-of-block code (value 256).  Then no
  224. // bytes need to be "returned" to the buffer at the end of the last
  225. // block.  See the huft_build() routine.
  226. //
  227.  
  228. #ifndef CHECK_EOF
  229. #  define CHECK_EOF   /* default as of 5.13/5.2 */
  230. #endif
  231.  
  232. #ifndef CHECK_EOF
  233. #  define NEEDBITS(n) {while(k<(n)){b|=((unsigned long)NEXTBYTE)<<k;k+=8;}}
  234. #else
  235. #  define NEEDBITS(n) {while(k<(n)){int c=NEXTBYTE;if(c==EOF)return 1; b|=((unsigned long)c)<<k;k+=8;}}
  236. #endif                      /* Piet Plomp:  change "return 1" to "break" */
  237.  
  238. #define DUMPBITS(n) {b>>=(n);k-=(n);}
  239.  
  240.  
  241. //
  242. // Huffman code lookup table entry--this entry is four bytes for machines
  243. // that have 16-bit pointers (e.g. PC's in the small or medium model).
  244. // Valid extra bits are 0..13.  e == 15 is EOB (end of block), e == 16
  245. // means that v is a literal, 16 < e < 32 means that v is a pointer to
  246. // the next table, which codes e - 16 bits, and lastly e == 99 indicates
  247. // an unused code.  If a code with e == 99 is looked up, this implies an
  248. // error in the data.
  249. //
  250. struct huft {
  251.     unsigned char e;                /* number of extra bits or operation */
  252.     unsigned char b;                /* number of bits in this code or subcode */
  253.     union {
  254.         unsigned short n;            /* literal, length base, or distance base */
  255.         struct huft *t;   /* pointer to next level of table */
  256.     } v;
  257. };
  258.  
  259. //
  260. // The inflate algorithm uses a sliding 32K byte window on the uncompressed
  261. // stream to find repeated byte strings.  This is implemented here as a
  262. // circular buffer.  The index is updated simply by incrementing and then
  263. // and'ing with 0x7fff (32K-1). */
  264. // It is left to other modules to supply the 32K area.  It is assumed
  265. // to be usable as if it were declared "uch slide[32768];" or as just
  266. // "uch *slide;" and then malloc'ed in the latter case.  The definition
  267. // must be in unzip.h, included above.
  268. //
  269. class Unzip
  270. {
  271. public:
  272.     static unsigned long global_bb;                         /* bit buffer */
  273.     static unsigned global_bk;                    /* bits in bit buffer */
  274.  
  275.     static unsigned global_wp;  /* current position in slide */
  276.     static unsigned global_hufts; /* huff memory usage */
  277.     static unsigned char slide_buffer[];
  278.     static struct huft *global_fixed_tl;    /* inflate static */
  279.     static struct huft *global_fixed_td;    /* inflate static */
  280.     static int global_fixed_bl,
  281.                global_fixed_bd;
  282. #if defined(UNIX_FILE_SYSTEM) || defined(AMIGAOS_FILE_SYSTEM)
  283.     static FILE *global_file; /* file pointer for zip file */
  284. #elif defined(WIN32_FILE_SYSTEM)
  285.     static char *global_file;
  286. #endif
  287.     static char *global_bufferp; /* current position in output buffer */
  288.  
  289.     /* Tables for deflate from PKZIP's appnote.txt. */
  290.     static unsigned border[];
  291.     static unsigned short cplens[];
  292.     static unsigned short cplext[]; /* Extra bits for literal codes 257..285 */
  293.     static unsigned short cpdist[]; /* Copy offsets for distance codes 0..29 */
  294.     static unsigned short cpdext[]; /* Extra bits for distance codes */
  295.  
  296.     /* moved to consts.h (included in unzip.c), resp. funzip.c */
  297.     /* And'ing with mask_bits[n] masks the lower n bits */
  298.     static unsigned short mask_bits[];
  299.  
  300.     //
  301.     // Huffman code decoding is performed using a multi-level table lookup.
  302.     // The fastest way to decode is to simply build a lookup table whose
  303.     // size is determined by the longest code.  However, the time it takes
  304.     // to build this table can also be a factor if the data being decoded
  305.     // are not very long.  The most common codes are necessarily the
  306.     // shortest codes, so those codes dominate the decoding time, and hence
  307.     // the speed.  The idea is you can have a shorter table that decodes the
  308.     // shorter, more probable codes, and then point to subsidiary tables for
  309.     // the longer codes.  The time it costs to decode the longer codes is
  310.     // then traded against the time it takes to make longer tables.
  311.     //
  312.     // This results of this trade are in the variables lbits and dbits
  313.     // below.  lbits is the number of bits the first level table for literal/
  314.     // length codes can decode in one step, and dbits is the same thing for
  315.     // the distance codes.  Subsequent tables are also less than or equal to
  316.     // those sizes.  These values may be adjusted either when all of the
  317.     // codes are shorter than that, in which case the longest code length in
  318.     // bits is used, or when the shortest code is *longer* than the requested
  319.     // table size, in which case the length of the shortest code in bits is
  320.     // used.
  321.     //
  322.     // There are two different values for the two tables, since they code a
  323.     // different number of possibilities each.  The literal/length table
  324.     // codes 286 possible values, or in a flat code, a little over eight
  325.     // bits.  The distance table codes 30 possible values, or a little less
  326.     // than five bits, flat.  The optimum values for speed end up being
  327.     // about one bit more than those, so lbits is 8+1 and dbits is 5+1.
  328.     // The optimum values may differ though from machine to machine, and
  329.     // possibly even between compilers.  Your mileage may vary.
  330.     //
  331.  
  332.     static int lbits;           /* bits in base literal/length lookup table */
  333.     static int dbits;           /* bits in base distance lookup table */
  334.  
  335.     static int huft_build(unsigned *b,unsigned n, unsigned s, unsigned short *d, unsigned short *e, struct huft **t, int *m);
  336.     static int huft_free(struct huft *);
  337.     static int inflate_codes(struct huft *tl,struct huft * td, int  bl,int bd);
  338.     static int inflate_stored();
  339.     static int inflate_fixed();
  340.     static int inflate_dynamic();
  341.     static int inflate_block(int *);
  342.     static int inflate_free();
  343.  
  344. #if defined(UNIX_FILE_SYSTEM) || defined(AMIGAOS_FILE_SYSTEM)
  345.     static int unzip8(FILE * zipfile, char *buffer);
  346.  
  347.     static int UncompressFile0(FILE *, char *, long);
  348.     static int UncompressFile1(FILE *, char *, long);
  349.     static int UncompressFile2(FILE *, char *, long);
  350.     static int UncompressFile3(FILE *, char *, long);
  351.     static int UncompressFile4(FILE *, char *, long);
  352.     static int UncompressFile5(FILE *, char *, long);
  353.     static int UncompressFile6(FILE *, char *, long);
  354.     static int UncompressFile7(FILE *, char *, long);
  355.     static int UncompressFile8(FILE *, char *, long);
  356.     static int UncompressFile9(FILE *, char *, long);
  357. #elif defined(WIN32_FILE_SYSTEM)
  358.     static int unzip8(char *zipfile, char *buffer);
  359.  
  360.     static int UncompressFile0(char *, char *, long);
  361.     static int UncompressFile1(char *, char *, long);
  362.     static int UncompressFile2(char *, char *, long);
  363.     static int UncompressFile3(char *, char *, long);
  364.     static int UncompressFile4(char *, char *, long);
  365.     static int UncompressFile5(char *, char *, long);
  366.     static int UncompressFile6(char *, char *, long);
  367.     static int UncompressFile7(char *, char *, long);
  368.     static int UncompressFile8(char *, char *, long);
  369.     static int UncompressFile9(char *, char *, long);
  370. #endif
  371. }; // end class unzip
  372.  
  373. #endif /* unzip_INCLUDED */
  374.